package com.qingcloud.adminbackend.controller;


import cn.hutool.core.bean.BeanUtil;
import com.alibaba.fastjson.JSON;
import com.qingcloud.adminbackend.base.AjaxResult;
import com.qingcloud.adminbackend.entity.aline.*;
import com.qingcloud.adminbackend.entity.aline.*;
import com.qingcloud.adminbackend.entity.aline.adapters.ABaseTcpCommandAdapter;
import com.qingcloud.adminbackend.entity.aline.adapters.BaseQueryTcpCommandAdapter;
import com.qingcloud.adminbackend.entity.aline.adapters.QueryTcpValueCommandAdapter;
import com.qingcloud.adminbackend.service.ISysSensorAlineAitectService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import static com.qingcloud.adminbackend.constant.WamingConstant.*;

/**
 * <p>
 * 网格化设备指令
 * </p>
 *
 * @author liuziyong
 * @since 2019-04-11
 */
@RestController
@RequestMapping("/sys/sensor/weather")
@Api(value = "网格化新协议指令", description = "网格化新协议指令")
public class SysSensorWeatherController {

    @Value("${file.rootpath}")
    private String ROOTPATH;

    @Resource
    private ISysSensorAlineAitectService sysSensorAlineAitectService;

    @ApiOperation(value = "参数指令查询下发", notes = "1:查询IP端口  \n" +
            "2:查询时间(unix时间戳)  \n" +
            "3:查询终端SIM卡号  \n" +
            "4:查询采集数据时间间隔(单位秒)  \n" +
            "5:查询低功耗模式下数据上传时间  \n" +
            "6:查询健康包上传时间间隔(单位分钟)  \n" +
            "9:查询PM2.5传感器主备  \n" +
            "10:查询短信配置合法号码  \n" +
            "11:查询硬件配置字  \n" +
            "12:查询蜂鸣器开启状态  \n" +
            "13:查询除湿模块工作阈值  \n" +
            "14:查询设备参数  \n" +
            "15:查询辅助IP  \n" +
            "16:查询辅助IP启用状态  \n" +
            "17:查询加热模块工作阈值  \n" +
            "18:查询泵使能  \n" +
            "19:查询颗粒物传感器模式  \n" +
            "20:查询采集状态  \n" +
            "24:查询特定时段内数据上传间隔  \n" +
            "26:查询设备名称  \n" +
            "27:查询数据分段功能启用状态  \n" +
            "28:查询光电板DAC值  \n" +
            "30:查询泵速  \n" +
            "31:查询PMT电源DAC值  \n" +
            "37:查询正常压差范围  \n" +
            "38:查询VOCS当前气体类型    \n" +
            "40:查询气溶胶采样器状态  \n" +
            "41:查询消毒灯状态  \n" +
            "192:工作模式查询  \n" +
            "208:工作状态查询(0休眠 1预热 2监测)  \n" +
            "209:预热时长查询(单位秒，范围55-240)  \n" +
            "210:域名和端口查询  \n" +
            "211:开机时间查询(unix时间戳)  \n" +
            "212:蓄电池电量查询")
    @PostMapping("/command/batch/query")
    public AjaxResult tcpQueryCommand(@RequestBody List<BaseTcpCommandDTO> baseQueryCommandDTO) {
        return sysSensorAlineAitectService.command(new BaseQueryTcpCommandAdapter(baseQueryCommandDTO));
    }

    @ApiOperation(value = "基础指令查询下发", notes = "0:设备复位,  \n" +
            "1:恢复出厂值指令,  \n" +
            "2:健康包状态查询,  \n" +
            "3:小区基站信息查询,  \n" +
            "4:GPS信息查询,  \n" +
            "5:设备关机,  \n" +
            "6:日志事件查询,  \n" +
            "7:器件寿命数据查询,  \n" +
            "8:传感器校准,")
    @PostMapping("/command/batch/baseCommand")
    public AjaxResult tcpBaseQueryCommand(@RequestBody List<BaseTcpCommandDTO> baseQueryCommandDTO) {
        return sysSensorAlineAitectService.command(new ABaseTcpCommandAdapter<List<BaseTcpCommandDTO>>(baseQueryCommandDTO) {

            @Override
            public String getUrl(String baseUrl) {
                return baseUrl + TCP_BATCH_BASE_COMMAND;
            }
        });
    }

    @ApiOperation(value = "参数指令查询下发带value", notes = "22:查询数据报警  \n" +
            "23:查询器件寿命  \n" +
            "25:查询传感器数据分段值  \n" +
            "29:查询采集板上报周期  \n" +
            "39:查询VOCS气体名称和固定系数(value: 23) 传感器类型：23-45区间\n" +
            "160:查询传感器应用修正系数  \n" +
            "176:查询传感器实验室修正系数  \n")
    @PostMapping("/command/batch/queryWithValue")
    public AjaxResult tcpQueryWithValueCommand(@RequestBody List<TcpValueCommandDTO> baseQueryCommandDTO) {
        return sysSensorAlineAitectService.command(new QueryTcpValueCommandAdapter(baseQueryCommandDTO));
    }

    @ApiOperation(value = "参数设置指令下发", notes = "1:设置IP端口 （value example: 127.0.0.1，21009）  \n" +
            "2:设置时间(unix时间戳)  \n" +
            "3:设置终端SIM卡号 （value example: 18304596843）  \n" +
            "4:设备采集数据时间间隔(单位秒)  \n" +
            "5:设置低功耗模式下数据上传时间  \n" +
            "6:设置健康包上传时间间隔(单位分钟)  \n" +
            "9:设置PM2.5传感器主备  \n" +
            "10:设置短信配置合法号码  \n" +
            "11:设置硬件配置字  \n" +
            "12:设置蜂鸣器开启状态(0/1)  \n" +
            "13:设置除湿模块工作阈值 (0-100)  \n" +
            "15:设置辅助IP  \n" +
            "16:设置辅助IP启用状态(0/1)  \n" +
            "17:设置加热模块工作阈值(-100-100)  \n" +
            "18:设置泵使能设置 (0/1)  \n" +
            "19:设置颗粒物传感器模式 (0/1)  \n" +
            "20:设置采集状态(0/1)  \n" +
            "37:设置正常压差范围 （value example: {\\\"alineStep1\\\":200,\\\"alineStep2\\\":20} 最高值200，最低值20  \n" +
            "38:设置VOCS当前气体类型 （value example: {\\\"type\\\":1}） Json 可选项： 23-45,8,11,12,14  \n" +
            "39:设置VOCS气体名称和固定系数 名称字节长度小于18 (value example: {\\\"sensorType\\\":23-45,\\\"coefficient\\\":1,\\\"k1\\\":1})  \n" +
            "40:设置气溶胶采样器状态 （value example: {\\\"status\\\":1}） Json 可选项： 0,1   \n" +
            "41:设置消毒灯状态 （value example: {\\\"lampStatus\\\":1,\\\"otherStatus\\\":1}） Json 可选项： 0,1  \n" +
            "42:设置气溶胶24通道DAC值 [{\"command\":42,\"equipmentId\":\"52004020\",\"value\":\"{\\\"aerosol1\\\":\\\"10\\\",\\\"aerosol2\\\":\\\"0\\\",\\\"aerosol3\\\":\\\"0\\\",\\\"aerosol4\\\":\\\"0\\\",\\\"aerosol5\\\":\\\"0\\\",\\\"aerosol6\\\":\\\"0\\\",\\\"aerosol7\\\":\\\"0\\\",\\\"aerosol8\\\":\\\"0\\\"}\"}]  \n" +
            "43:气溶胶24通道校准步长设置 [{\"command\":43,\"equipmentId\":\"52004020\",\"value\":\"{\\\"aerosol1\\\":\\\"10\\\",\\\"aerosol2\\\":\\\"0\\\",\\\"aerosol3\\\":\\\"0\\\",\\\"aerosol4\\\":\\\"0\\\",\\\"aerosol5\\\":\\\"0\\\",\\\"aerosol6\\\":\\\"0\\\",\\\"aerosol7\\\":\\\"0\\\",\\\"aerosol8\\\":\\\"0\\\"}\"}]  \n" +
            "44:气溶胶24通道校准精度设置 [{\"command\":44,\"equipmentId\":\"52004020\",\"value\":\"{\\\"aerosol1\\\":\\\"10\\\",\\\"aerosol2\\\":\\\"0\\\",\\\"aerosol3\\\":\\\"0\\\",\\\"aerosol4\\\":\\\"0\\\",\\\"aerosol5\\\":\\\"0\\\",\\\"aerosol6\\\":\\\"0\\\",\\\"aerosol7\\\":\\\"0\\\",\\\"aerosol8\\\":\\\"0\\\"}\"}]  \n" +
            "176:设置传感器实验室修正系数  \n" +
            "192:设置工作模式 (0/1/2/3)  \n" +
            "208:(YYT)工作状态设置(0休眠 1预热 2监测)  \n" +
            "209:(YYT)预热时长设置(单位秒，范围55-240),  \n" +
            "210:(YYT)域名和端口设置(暂时不支持）\n")
    @PostMapping("/command/batch/setWithValue")
    public AjaxResult tcpSetCommand(@RequestBody List<TcpValueCommandDTO> baseQueryCommandDTO) {
        return sysSensorAlineAitectService.command(new ABaseTcpCommandAdapter<List<TcpValueCommandDTO>>(baseQueryCommandDTO) {

            @Override
            public String getUrl(String baseUrl) {
                return baseUrl + TCP_BATCH_SET_COMMAND;
            }
        });
    }

    @ApiOperation(value = "设置时间(unix时间戳)", notes = "2:设置时间(unix时间戳)")
    @PostMapping("/command/batch/setTime")
    public AjaxResult setTime(@RequestBody List<BaseTcpCommandDTO> baseTcpCommandDTOS) {
        return sysSensorAlineAitectService.command(new ABaseTcpCommandAdapter<List<BaseTcpCommandDTO>>(baseTcpCommandDTOS) {
            @Override
            public String toReceiveJson() {
                final ArrayList<TcpValueCommandDTO> objects = new ArrayList<>();
                baseTcpCommandDTOS.stream().forEach(baseTcpCommandDTO -> {
                    final TcpValueCommandDTO e = new TcpValueCommandDTO();
                    BeanUtils.copyProperties(baseTcpCommandDTO, e);
                    e.setValue(System.currentTimeMillis() / 1000L);
                    objects.add(e);
                });
                return JSON.toJSONString(objects);
            }

            @Override
            public String getUrl(String baseUrl) {
                return baseUrl + TCP_BATCH_SET_COMMAND;
            }
        });
    }


    @ApiOperation(value = "传感器修正指令", notes = "160:查询传感器应用修正系数  \n" +
            "176:查询传感器实验室修正系数")
    @PostMapping("/command/batch/querySensorSubValue")
    public AjaxResult tcpQuerySensorSubValueCommand(@RequestBody List<QueryTcpSensorSubCommandDTO> baseQueryCommandDTO) {
        return sysSensorAlineAitectService.command(new ABaseTcpCommandAdapter<List<QueryTcpSensorSubCommandDTO>>(baseQueryCommandDTO) {
            @Override
            public String toReceiveJson() {
                final ArrayList<TcpValueCommandDTO> tcpValueCommandDTOS = new ArrayList<>();
                for (QueryTcpSensorSubCommandDTO queryTcpSensorSubCommandDTO : baseQueryCommandDTO) {
                    if (queryTcpSensorSubCommandDTO == null)
                        continue;
                    final TcpValueCommandDTO tcpValueCommandDTO = new TcpValueCommandDTO();
                    BeanUtil.copyProperties(queryTcpSensorSubCommandDTO, tcpValueCommandDTO);
                    final List<QueryTcpSensorSubCommandDTO.QueryTcpSensorSubCommandDTOValue> object = Arrays.asList(queryTcpSensorSubCommandDTO.getValue());
                    tcpValueCommandDTO.setValue(JSON.toJSONString(object));
                    tcpValueCommandDTOS.add(tcpValueCommandDTO);
                }
                return JSON.toJSONString(tcpValueCommandDTOS);
            }

            @Override
            public String getUrl(String baseUrl) {
                return baseUrl + TCP_BATCH_QUERY_COMMAND;
            }
        });
    }

    @ApiOperation(value = "设置数据报警", notes = "22:设置数据报警")
    @PostMapping("/command/batch/setDataWarn")
    public AjaxResult setDataWarnCommand(@RequestBody List<SetTcpDataWarnCommandDTO> baseQueryCommandDTO) {
        return sysSensorAlineAitectService.command(new ABaseTcpCommandAdapter<List<SetTcpDataWarnCommandDTO>>(baseQueryCommandDTO) {
            @Override
            public String toReceiveJson() {
                final ArrayList<TcpValueCommandDTO> tcpValueCommandDTOS = new ArrayList<>();
                for (SetTcpDataWarnCommandDTO queryTcpSensorSubCommandDTO : baseQueryCommandDTO) {
                    if (queryTcpSensorSubCommandDTO == null)
                        continue;
                    final TcpValueCommandDTO tcpValueCommandDTO = new TcpValueCommandDTO();
                    BeanUtil.copyProperties(queryTcpSensorSubCommandDTO, tcpValueCommandDTO);
                    tcpValueCommandDTO.setValue(JSON.toJSONString(queryTcpSensorSubCommandDTO.getValue()));
                    tcpValueCommandDTOS.add(tcpValueCommandDTO);
                }
                return JSON.toJSONString(tcpValueCommandDTOS);
            }

            @Override
            public String getUrl(String baseUrl) {
                return baseUrl + TCP_BATCH_SET_COMMAND;
            }
        });
    }

    @ApiOperation(value = "设置器件寿命", notes = "23:设置器件寿命")
    @PostMapping("/command/batch/setEquipmentLife")
    public AjaxResult setEquipmentLifeCommand(@RequestBody List<SetEquipmentLifeCommandDTO> baseQueryCommandDTO) {
        return sysSensorAlineAitectService.command(new ABaseTcpCommandAdapter<List<SetEquipmentLifeCommandDTO>>(baseQueryCommandDTO) {
            @Override
            public String toReceiveJson() {
                final ArrayList<TcpValueCommandDTO> tcpValueCommandDTOS = new ArrayList<>();
                for (SetEquipmentLifeCommandDTO queryTcpSensorSubCommandDTO : baseQueryCommandDTO) {
                    if (queryTcpSensorSubCommandDTO == null)
                        continue;
                    final TcpValueCommandDTO tcpValueCommandDTO = new TcpValueCommandDTO();
                    BeanUtil.copyProperties(queryTcpSensorSubCommandDTO, tcpValueCommandDTO);
                    tcpValueCommandDTO.setValue(JSON.toJSONString(queryTcpSensorSubCommandDTO.getValue()));
                    tcpValueCommandDTOS.add(tcpValueCommandDTO);
                }
                return JSON.toJSONString(tcpValueCommandDTOS);
            }

            @Override
            public String getUrl(String baseUrl) {
                return baseUrl + TCP_BATCH_SET_COMMAND;
            }
        });
    }

    @ApiOperation(value = "设置特定时段内数据上传间隔 ", notes = "24:设置特定时段内数据上传间隔 ")
    @PostMapping("/command/batch/setUploadInterval")
    public AjaxResult setUploadInterval(@RequestBody List<SetUploadIntervalCommandDTO> baseQueryCommandDTO) {
        return sysSensorAlineAitectService.command(new ABaseTcpCommandAdapter<List<SetUploadIntervalCommandDTO>>(baseQueryCommandDTO) {
            @Override
            public String toReceiveJson() throws NoSuchFieldException, IllegalAccessException {
                final ArrayList<TcpValueCommandDTO> tcpValueCommandDTOS = new ArrayList<>();
                for (SetUploadIntervalCommandDTO queryTcpSensorSubCommandDTO : baseQueryCommandDTO) {
                    if (queryTcpSensorSubCommandDTO == null)
                        continue;
                    final TcpValueCommandDTO tcpValueCommandDTO = new TcpValueCommandDTO();
                    BeanUtil.copyProperties(queryTcpSensorSubCommandDTO, tcpValueCommandDTO);
                    tcpValueCommandDTO.setValue(JSON.toJSONString(queryTcpSensorSubCommandDTO.getValue()));
                    tcpValueCommandDTOS.add(tcpValueCommandDTO);
                }
                return JSON.toJSONString(tcpValueCommandDTOS);
            }

            @Override
            public String getUrl(String baseUrl) {
                return baseUrl + TCP_BATCH_SET_COMMAND;
            }
        });
    }

    @ApiOperation(value = "设置传感器数据分段值 ", notes = "25:设置传感器数据分段值 ")
    @PostMapping("/command/batch/setDataSection")
    public AjaxResult setDataSection(@RequestBody List<SetDataSectionCommandDTO> baseQueryCommandDTO) {
        return sysSensorAlineAitectService.command(new ABaseTcpCommandAdapter<List<SetDataSectionCommandDTO>>(baseQueryCommandDTO) {
            @Override
            public String toReceiveJson() throws NoSuchFieldException, IllegalAccessException {
                final ArrayList<TcpValueCommandDTO> tcpValueCommandDTOS = new ArrayList<>();
                for (SetDataSectionCommandDTO queryTcpSensorSubCommandDTO : baseQueryCommandDTO) {
                    if (queryTcpSensorSubCommandDTO == null)
                        continue;
                    final TcpValueCommandDTO tcpValueCommandDTO = new TcpValueCommandDTO();
                    BeanUtil.copyProperties(queryTcpSensorSubCommandDTO, tcpValueCommandDTO);
                    tcpValueCommandDTO.setValue(JSON.toJSONString(queryTcpSensorSubCommandDTO.getValue()));
                    tcpValueCommandDTOS.add(tcpValueCommandDTO);
                }
                return JSON.toJSONString(tcpValueCommandDTOS);
            }

            @Override
            public String getUrl(String baseUrl) {
                return baseUrl + TCP_BATCH_SET_COMMAND;
            }
        });
    }

    @ApiOperation(value = "设置传感器应用修正系数 ", notes = "160:设置传感器应用修正系数 ")
    @PostMapping("/command/batch/setSensorCorrectionFactor")
    public AjaxResult setSensorCorrectionFactor(@RequestBody List<SetSensorCorrectionFactorCommandDTO> baseQueryCommandDTO) {
        return sysSensorAlineAitectService.command(new ABaseTcpCommandAdapter<List<SetSensorCorrectionFactorCommandDTO>>(baseQueryCommandDTO) {
            @Override
            public String toReceiveJson() {
                final ArrayList<TcpValueCommandDTO> tcpValueCommandDTOS = new ArrayList<>();
                for (SetSensorCorrectionFactorCommandDTO queryTcpSensorSubCommandDTO : baseQueryCommandDTO) {
                    if (queryTcpSensorSubCommandDTO == null)
                        continue;
                    final TcpValueCommandDTO tcpValueCommandDTO = new TcpValueCommandDTO();
                    BeanUtil.copyProperties(queryTcpSensorSubCommandDTO, tcpValueCommandDTO);
                    tcpValueCommandDTO.setValue(JSON.toJSONString(queryTcpSensorSubCommandDTO.getValue()));
                    tcpValueCommandDTOS.add(tcpValueCommandDTO);
                }
                return JSON.toJSONString(tcpValueCommandDTOS);
            }

            @Override
            public String getUrl(String baseUrl) {
                return baseUrl + TCP_BATCH_SET_COMMAND;
            }
        });
    }
    @ApiOperation(value = "佳沃天河消杀设备指令下发", notes = "佳沃天河消杀设备指令下发ctrlCode：0：关闭。1开启。")
    @PostMapping("/command/batch/jwthKill")
    public AjaxResult jwthKill(@RequestBody List<BZJwthDTO> bzJwthDTOList) {
        return sysSensorAlineAitectService.jwthCommand(bzJwthDTOList);
    }

}

